using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Fieldbook.Entities;
using System.Collections.Generic;
using Fieldbook.Business;

namespace Fieldbook.Testwebsite.User
{
    public partial class AddStorageTransaction : FieldbookPage
    {
        protected override void Page_Load(object sender, EventArgs e)
        {
            ReddirectNonActiveSession();
            if (ddlTranactionType.Items.Count == 0)
            {
                foreach (Fieldbook.Entities.StorageTranaction.StorageTransactionType value in StorageTranaction.StorageTransactionType.GetValues(typeof(Fieldbook.Entities.StorageTranaction.StorageTransactionType)))
                {
                    ddlTranactionType.Items.Add(
                        new ListItem(value.ToString(),
                            ((byte)value).ToString()));
                }
            }
        }

        protected void ddlResourceCategory1_SelectedIndexChanged(object sender, EventArgs e)
        {
            long userID = (long)Session["userID"];
            if (!String.IsNullOrEmpty(ddlResourceCategory1.SelectedValue))
            {
                long categoryID = Int64.Parse(ddlResourceCategory1.SelectedValue);
                List<Resource> resources = ResourceController.LoadResources(userID, categoryID);
                ddlResourceItem.DataSource = resources;
                ddlResourceItem.DataBind();
            }
        }

        /// <summary>
        /// Add new resource transaction
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void linkAddResourceTransacton_Command(object sender, CommandEventArgs e)
        {
            bool sucess = false;
            long userID = (long)Session["userID"];
            long resourceID = Int64.Parse(this.ddlResourceItem.SelectedValue);
            StorageTranaction.StorageTransactionType sTransactionType =
                (StorageTranaction.StorageTransactionType)Byte.Parse(this.ddlTranactionType.SelectedValue);
            decimal pricePerUnit = Decimal.Parse(this.txtPricePerUnit.Text);
            decimal quota = Decimal.Parse(this.txtQuota.Text);
            decimal total = Decimal.Parse(this.txtTotal.Text);

            sucess = StorageController.InsertStorageTransaction(userID, resourceID, pricePerUnit, quota, total, sTransactionType);

            if (sucess)
            {
                lblOperationStatus.Text = "Resource sucessfully added";
                this.txtPricePerUnit.Text = "";
                this.txtQuota.Text = "";
                this.txtTotal.Text = "";
                Response.Redirect("~/User/StorageOverview.aspx");
            }
            else
            {
                lblOperationStatus.Text = "Error adding resource";
            }
        }
    }
}
